home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / qmgr / cons-misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.7 KB  |  201 lines

  1. /* cons-misc.c: misc routines */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/qmgr/RCS/cons-misc.c,v 6.0 1991/12/18 20:23:58 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/qmgr/RCS/cons-misc.c,v 6.0 1991/12/18 20:23:58 jpo Rel $
  9.  *
  10.  * $Log: cons-misc.c,v $
  11.  * Revision 6.0  1991/12/18  20:23:58  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15. #include "util.h"
  16. #include <sys/time.h>
  17. #include "qmgr-int.h"
  18. #include "Qmgr-types.h"
  19.  
  20.  
  21. /*   */
  22.  
  23. time_t convert_time(qb)
  24. struct type_UNIV_UTCTime    *qb;
  25. {
  26.     char    *str;
  27.     time_t    temp;
  28.     if (qb == NULL)
  29.         return 0;
  30.     str = qb2str(qb);
  31.     temp = utc2time_t(str2utct(str, strlen(str)));
  32.     free(str);
  33.     return temp;
  34. }
  35.  
  36. /*   */
  37.  
  38. ProcStatus    *convert_ProcStatus(pepsy)
  39. register struct type_Qmgr_ProcStatus    *pepsy;
  40. {
  41.     ProcStatus    *ret = (ProcStatus *) malloc(sizeof(*ret));
  42.     
  43.     ret->enabled = pepsy->enabled;
  44.     ret->lastAttempt = convert_time(pepsy->lastAttempt);
  45.     ret->cachedUntil = convert_time(pepsy->cachedUntil);
  46.     ret->lastSuccess = convert_time(pepsy->lastSuccess);
  47.     return ret;
  48. }
  49. /*   */
  50.  
  51. long     parseUnitStr (s, isTime)
  52. char    *s;
  53. int    isTime;
  54. {
  55.     long n;
  56.     if (s == NULLCP || *s == NULL) return 0;
  57.     while(*s != NULL && isspace(*s)) s++;
  58.     n = 0;
  59.     while (*s != NULL && isdigit(*s)) {
  60.         n = n * 10 + *s - '0';
  61.         s++;
  62.     }
  63.     while (*s != NULL && isspace(*s)) s++;
  64.     if (*s != NULL && isalpha(*s)) {
  65.         switch (*s) {
  66.             case 's': /* seconds */
  67.             case 'S': 
  68.             break;
  69.             case 'm':  /* minutes */
  70.             case 'M':  /* or millions */
  71.             if (isTime == TRUE)
  72.                 n *= 60; 
  73.             else
  74.                 n *= 1000000;
  75.             break;
  76.             case 'h': /* hours */
  77.             case 'H':
  78.             n *= 3600; 
  79.             break;
  80.             case 'd': /* days */
  81.             case 'D':
  82.             n *= 86400; 
  83.             break;
  84.             case 'w': /* weeks */
  85.             case 'W':
  86.             n *= 604800;
  87.             break;
  88.             case 'k': /* kilo */
  89.             case 'K':
  90.             n *= 1000;
  91.             break;
  92.             default:
  93.             break;
  94.         }
  95.         return n + parseUnitStr(s+1, 1);
  96.     }
  97.     else return n + parseUnitStr(s, 1);
  98. }
  99.  
  100. /*   */
  101.  
  102. char    *unparseUnitStr(n, isTime)
  103. long    n;
  104. int    isTime;
  105. {
  106.     char    buf[BUFSIZ];
  107.     buf[0] = '\0';
  108.     if (isTime == TRUE) {
  109.         unsigned long    res;
  110.         if ((res = n / (60 * 60 * 24)) != 0) {
  111.             (void) sprintf(buf, "%d d", res);
  112.             n = n % (60 * 60 * 24);
  113.         }
  114.         if ((res = n / (60 * 60)) != 0) {
  115.             (void) sprintf(buf,
  116.                 ('\0' == buf[0]) ? "%s%d h" : "%s %d h",
  117.                 buf, res);
  118.             n = n % (60 * 60);
  119.         }
  120.         if ((res = n / 60) != 0) {
  121.             (void) sprintf(buf, 
  122.                 ('\0' == buf[0]) ? "%s%d m" : "%s %d m",
  123.                 buf, res);
  124.             n = n % 60;
  125.         }
  126.         
  127.         if (0 != n)
  128.             (void) sprintf(buf, 
  129.                 ('\0' == buf[0]) ? "%s%d s" : "%s %d s",
  130.                 buf, n);
  131.     } else {
  132.         /* print to largest unit with 2 significant figures */
  133.         long    absolute = (n < 0) ? -n : n;
  134.  
  135.         if (absolute > 1000000)
  136.             (void) sprintf(buf, "%s%.2f M", 
  137.                        (n < 0) ? "-" : "",
  138.                        (double) absolute / 1000000.0);
  139.         else if (absolute > 1000)
  140.             (void) sprintf(buf, "%s%.2f k",
  141.                        (n < 0) ? "-" : "",
  142.                        (double) absolute / 1000.0);
  143.         else
  144.             (void) sprintf(buf, "%s%d", 
  145.                        (n < 0) ? "-" : "",
  146.                        absolute);
  147.     }
  148.     return strdup(buf);
  149. }
  150.  
  151. /*   */
  152.  
  153. char *mystrtotime(str)
  154. char    *str;
  155. {
  156.     UTC    utc;
  157.     time_t    newsecs;
  158.     time_t     current;
  159.     char    *retval;
  160.  
  161.     newsecs = (time_t) parseUnitStr(str, TRUE);
  162.     (void) time(¤t);
  163.     
  164.     current += newsecs;
  165.         
  166.     utc = time_t2utc(current);
  167.  
  168.     retval = utct2str(utc);
  169.     free((char *) utc);
  170.     return strdup(retval);
  171. }
  172.  
  173. /*   */
  174.  
  175. char    *time_t2RFC(in)
  176. time_t    in;
  177. {
  178.     char    buf[BUFSIZ];
  179.     struct tm *tm;
  180.     struct UTCtime uts;
  181. #ifdef SVR4
  182.     extern long timezone;
  183. #else
  184.     struct timeval dummy;
  185.     struct timezone time_zone;
  186. #endif
  187.     
  188.     tm = localtime (&in);
  189.     tm2ut (tm, &uts);
  190.     uts.ut_flags |= UT_ZONE;
  191. #ifdef SVR4
  192.     uts.ut_zone = timezone/60;
  193. #else
  194.     (void) gettimeofday(&dummy, &time_zone);
  195.     uts.ut_zone = time_zone.tz_minuteswest;
  196. #endif
  197.     if (UTC2rfc(&uts, buf) == OK)
  198.         return strdup(buf);
  199.     return NULLCP;
  200. }
  201.